package recordService

import (
	"gin/dao/entity"
	"gin/dao/mapper/exercise"
	"gin/dao/mapper/record"
	"gin/model/body"
	"gin/service/base"
	error2 "gin/util/error"
)

// Add 添加运动记录
func Add(p body.AddRecordParam) {
	info := record.IsDoingCount(p.Uid)
	if info.Id > 0 {
		error2.CustomPanic("正在运动中请勿重复点击！")
	}
	p.ExerciseStatus = 1
	record.Add(p)
}

// Complete 完成任务
func Complete(uid int64, p body.CompleteRecordParam) {
	if p.Id == 0 {
		info := record.IsDoingCount(uid)
		p.Id = info.Id
	}
	if p.Id == 0 {
		return
	}
	record.Complete(uid, p)
}

// Detail 运动详情
func Detail(uid, id int64) entity.RecordDetail {
	info := entity.RecordDetail{}
	if id == 0 {
		re := record.GetNew(uid)
		id = re.Id
	}
	if id == 0 {
		return info
	}

	info.Record = record.Detail(uid, id)
	ex := exercise.Detail(info.ExerciseId)
	info.ExerciseName = ex.Name
	info.ExerciseImgUrl = base.CompleteImgUrl(ex.ImgUrl)
	return info
}

// List 运动记录列表
func List(p body.RecordListParam) ([]*entity.RecordDetail, int64) {
	list, count := record.List(p)
	for _, v := range list {
		v.ExerciseImgUrl = base.CompleteImgUrl(v.ExerciseImgUrl)
	}
	return list, count
}

// Del 删除运动记录
func Del(uid, id int64) {
	if id == 0 {
		info := record.IsDoingCount(uid)
		id = info.Id
	}
	if id == 0 {
		return
	}
	record.Del(uid, id)
}
